package com.mathpractise.question.generator;

import com.mathpractise.model.Question;
import com.mathpractise.model.TrainingConfig;
import com.mathpractise.model.enums.QuestionType;
import com.mathpractise.util.RandomUtil;
import org.springframework.stereotype.Component;

/**
 * 两位数加减1位数题目生成器
 */
@Component
public class TwoDigitAddSubOneDigitGenerator extends AbstractQuestionGenerator {
    
    @Override
    public QuestionType getSupportedType() {
        return QuestionType.TWO_DIGIT_ADD_SUB_ONE_DIGIT;
    }
    
    @Override
    protected Question doGenerateQuestion(TrainingConfig trainingConfig) {
        // 随机决定是加法还是减法
        boolean isAddition = RandomUtil.nextBoolean();
        
        if (isAddition) {
            // 两位数加一位数
            int num1 = RandomUtil.nextInt(10, 100); // 10-99之间的两位数
            int num2 = RandomUtil.nextInt(1, 10);   // 1-9之间的一位数
            
            String expression = num1 + "+" + num2;
            int correctAnswer = num1 + num2;
            
            Question question = new Question();
            question.setExpression(expression);
            question.setCorrectAnswer(correctAnswer);
            
            return question;
        } else {
            // 两位数减一位数
            int num1 = RandomUtil.nextInt(10, 100); // 10-99之间的两位数
            int num2 = RandomUtil.nextInt(1, 10);   // 1-9之间的一位数
            
            // 确保结果不是负数且不为0
            if (num1 <= num2) {
                num1 = num2 + RandomUtil.nextInt(1, 10); // 确保结果至少为1
            }
            
            String expression = num1 + "-" + num2;
            int correctAnswer = num1 - num2;
            
            Question question = new Question();
            question.setExpression(expression);
            question.setCorrectAnswer(correctAnswer);
            
            return question;
        }
    }
}